using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeFrame.Core;
using WeFrame.Core.Common;
using WeFrame.Core.BLL;
using WeFrame.Core.Model;
using WeFrame.Test.Model;
using WeFrame.Test.BLL;

namespace WeFrame.Web.Admin.Test
{
    public partial class TestStudentEdit :  WeFrame.Core.UI.AdminPage
    {
        private string action;//操作类型
        private int id = 0;
        TestStudentBLL bll = new TestStudentBLL();
        TestStudentEntity model;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                string action = WeFrameRequest.GetQueryString("action");
                HiddenFieldAction.Value = action;

                ChkAdminLevel("TestStudent", WeFrameEnums.ActionEnum.View.ToString()); //检查权限

                
                
                if (action == WeFrameEnums.ActionEnum.Edit.ToString())
                {
                    if (!int.TryParse(Request.QueryString["Id"] as string, out id))
                    {
                        JscriptMsg("传输参数不正确！", "back");
                        return;
                    }

                    if (!bll.Exists(id))
                    {
                        JscriptMsg("记录不存在或已被删除！", "back");
                        return;
                    }

                    HiddenFieldId.Value = id.ToString();

                    ShowInformation(id);
                
                }


            }
        }



        #region 赋值操作=================================
        private void ShowInformation(int id)
        {
            model = bll.GetModel(id);
            if(model.Name!=null)
            {
                 TextBoxName.Text = model.Name.ToString();
            }
            TextBoxAge.Text = model.Age.ToString();
            if(model.Remark!=null)
            {
                 TextBoxRemark.Text = model.Remark.ToString();
            }
        }
        #endregion
          
        #region 增加操作=================================
        private bool DoAdd()
        {
            TestStudentEntity model = new TestStudentEntity();
            TestStudentBLL bll = new TestStudentBLL();
            model.Name=TextBoxName.Text;
            model.Age=int.Parse(TextBoxAge.Text);
            model.CreateBy=this.GetSystemAdmin().Id;
            model.CreateTime=DateTime.Now;
            model.ModifyBy=model.CreateBy;
            model.ModifyTime=model.ModifyTime;
            model.Remark=TextBoxRemark.Text;

            if (bll.Add(model) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int id)
        {
            bool result = false;
            model = bll.GetModel(id);
            model.Name=TextBoxName.Text;
            model.Age=int.Parse(TextBoxAge.Text);
            model.ModifyBy=this.GetSystemAdmin().Id;
            model.ModifyTime=DateTime.Now;
            model.Remark=TextBoxRemark.Text;

            if (bll.Update(model))
            {
               
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {

            if (HiddenFieldAction.Value == WeFrameEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("TestStudent", WeFrameEnums.ActionEnum.Edit.ToString()); //检查权限
                id = int.Parse( HiddenFieldId.Value);
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改管理员信息成功！", "TestStudentList.aspx");
            }
            else if (HiddenFieldAction.Value == WeFrameEnums.ActionEnum.Add.ToString())//添加
            {
                ChkAdminLevel("ManagerList", WeFrameEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加管理员信息成功！", "TestStudentList.aspx");
            }
        }
    }
}

